import axios from 'axios';
import router from '../router/index.js'

const newAxios = axios.create({
    // baseURL:'http://192.168.14.175:8848',
    // baseURL:'http://localhost:8848',

    baseURL:'http://47.98.128.191:3001',

    timeout:3000,
});

// -------------------------------axios 响应拦截器--------------------------------
newAxios.interceptors.response.use(res=>{
    //------------------请求成功的响应------------------------------
    return res.data
},(err)=>{
    if(err && err.response && err.response.status ){
        switch (err.response.status ) {
            case 401:
                alert('你还未登录或者登录超时，请重新登录！');
                router.push('/login')
                break;
            case 404:
                alert('你访问的文件已经不存在了~~')
                break;
            default:
                err.message = '服务器出错'
                break;
        }
        
    }else {
       err.message = '服务器出错'
    }
    return promise.reject(err.message)
    // console.log(err.response.status)
});

//---------------------------------axios 请求拦截器---------------------------------
newAxios.interceptors.request.use((config) =>{
    //--------------------------------拦截成功-------------------------------------
    const token = localStorage.token;
    config.headers.Authorization = token;
    return config
},err=>{
    //--------------------------------拦截失败----------------------------------
    // console.log(err)
    return err
})








export default newAxios
















